﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;


namespace QuanLyKho
{
    public partial class frmKhoHang : Form
    {
        private DataTable dtKho, dtVatTu;
        private TaiKhoan taiKhoan = new TaiKhoan("admin", "admin", "admin");
        private string[] danhSachXoaKho;
        private int sttdanhSachXoaKho;

        public frmKhoHang()
        {
            InitializeComponent();
        }

        private void frmKhoHang_Load(object sender, EventArgs e)
        {
            hienThiKho();
        }

        private void hienThiKho()
        {
            DataSet ds = connection.FillDataSet("proc_getDanhSachKho", CommandType.StoredProcedure, null, null, taiKhoan);

            dtKho = ds.Tables[0];

            gctKho.DataSource = dtKho;
            gclMaKho.FieldName = dtKho.Columns["MaKho"].ColumnName;
            gclTenKho.FieldName = dtKho.Columns["TenKho"].ColumnName;

            danhSachXoaKho = new string[dtKho.Rows.Count];
            sttdanhSachXoaKho = 0;
        }

        private void hienThiVatTu(string maKho)
        {
            try
            {
                string[] para = { "@MaKho" };
                string[] values = { maKho };
                DataSet ds = connection.FillDataSet("proc_getDanhSachHangHoaTrongKho", CommandType.StoredProcedure, para, values, taiKhoan);
                dtVatTu = ds.Tables[0];
                DataColumn workCol = dtVatTu.Columns.Add("Chon", typeof(Boolean));
                workCol.DefaultValue = "false";

                gctVatTu.DataSource = dtVatTu;
                gclChon.FieldName = dtVatTu.Columns["Chon"].ColumnName;
                gclTenVatTu.FieldName = dtVatTu.Columns["TenVatTu"].ColumnName;
                gclSoLuongTonKho.FieldName = dtVatTu.Columns["SoLuong"].ColumnName;
            }
            catch { }
        }

        private void picKhoAdd_Click(object sender, EventArgs e)
        {
            string[] para = { "@MaKho", "@TenKho" };
            string[] values = { txtMaKho.Text.Trim(), txtTenKho.Text.Trim() };

            connection.Excute("proc_insertKho", CommandType.StoredProcedure, para, values, taiKhoan);
            dtKho.Rows.Add(txtMaKho.Text.Trim(), txtTenKho.Text.Trim(), XuLy.convertToUnSign3(txtTenKho.Text.Trim()));
            picKhoClean_Click(sender, e);
        }

        private void picKhoClean_Click(object sender, EventArgs e)
        {
            txtTenKho.Text = txtMaKho.Text = "";
            txtMaKho.Focus();
        }

        private void frmKhoHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (picKhoAdd.Enabled)
                    picKhoAdd_Click(sender, e);
        }

        private void gctKho_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                try
                {
                    if (gvKho.GetDataRow(gvKho.GetSelectedRows()[0])["MaKho"].ToString() != "KT")
                    {
                        ContextMenu m = new ContextMenu();
                        m.MenuItems.Add(new MenuItem("Xóa", menuXoa));
                        m.Show(gctKho, new Point(e.X, e.Y));
                    }
                }
                catch { }
            }
            else
            {
                hienThiVatTu(gvKho.GetDataRow(gvKho.GetSelectedRows()[0])["MaKho"].ToString());
            }
        }

        private void menuXoa(object sender, EventArgs e)
        {
            //if (isKho)//Kho
            {
                if (MessageBox.Show("Bạn có muốn xóa \"" + dtKho.Rows[gvKho.GetSelectedRows()[0]]["TenKho"].ToString() + "\" ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string[] para = { "@MaKho" };
                    string[] values = { dtKho.Rows[gvKho.GetSelectedRows()[0]]["MaKho"].ToString() };
                    connection.Excute("proc_deleteKho", CommandType.StoredProcedure, para, values, taiKhoan);

                    gvKho.DeleteRow(gvKho.GetSelectedRows()[0]);
                }
            }
            /*else//Vật tư
            {
                int[] itemChon = gvDonViTinh.GetSelectedRows();
                if (!Convert.ToBoolean(gvDonViTinh.GetDataRow(itemChon[0])["LaMacDinh"].ToString()))
                {
                    danhSachXoaDonViTinh[sttdanhSachXoaDonViTinh] = dsDonViTinh.Tables[0].Rows[itemChon[0]]["STT"].ToString();
                    sttdanhSachXoaDonViTinh++;
                    gvDonViTinh.DeleteRow(itemChon[0]);
                }
                else
                    MessageBox.Show("Không thể xóa đơn vị tính mặc định", "Thông báo");
            }*/
        }

        private void gvKho_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DataRow dr = dtKho.Rows[e.RowHandle];
            if (dr["MaKho"].ToString() != "KT")
            {
                string[] para = { "@MaKho", "@TenKho" };
                string[] values = { dr["MaKho"].ToString(), dr["TenKho"].ToString() };

                connection.Excute("proc_updateKho", CommandType.StoredProcedure, para, values, taiKhoan);
                DataRow[] customerRow = dtKho.Select("MaKho = '" + dr["MaKho"].ToString() + "'");
                customerRow[0]["Tên Kho"] = XuLy.convertToUnSign3(dr["TenKho"].ToString());
            }
            else
            {
                MessageBox.Show("Không thể chỉnh sửa kho tổng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                gvKho.SetRowCellValue(e.RowHandle, gclTenKho, "Kho Tổng");
            }
        }

        private void gvKho_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gvKho.GetSelectedRows()[0])
                e.Appearance.BackColor = Color.SkyBlue;
        }

        private void txtMaKho_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                gvKho.Columns["MaKho"].FilterInfo = new ColumnFilterInfo("Contains([MaKho], '" + txtMaKho.Text.Trim() + "')");

                if (txtMaKho.Text != "" && txtTenKho.Text != "")
                    picKhoAdd.Enabled = picKhoAdd.Visible = true;
                else
                    picKhoAdd.Enabled = picKhoAdd.Visible = false;

                if (txtMaKho.Text.Trim() == "")
                    gvKho.Columns["MaKho"].ClearFilter();
            }
            catch { }
        }

        private void txtTenKho_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                gvKho.Columns["TenKho"].FilterInfo = new ColumnFilterInfo("Contains([Tên Kho], '" + XuLy.convertToUnSign3(txtTenKho.Text.Trim()) + "')");

                if (txtMaKho.Text != "" && txtTenKho.Text != "")
                    picKhoAdd.Enabled = picKhoAdd.Visible = true;
                else
                    picKhoAdd.Enabled = picKhoAdd.Visible = false;

                if (txtTenKho.Text.Trim() == "")
                    gvKho.Columns["TenKho"].ClearFilter();
            }
            catch { }
        }

        private void gvVatTu_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (gvVatTu.GetDataRow(e.RowHandle)["Chon"].ToString() == "True")
                e.Appearance.BackColor = Color.SkyBlue;
        }

        private void gctVatTu_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                try
                {
                    bool dangChon = false;
                    for (int i = 0; i < gvVatTu.RowCount; i++)
                    {
                        if (gvVatTu.GetDataRow(i)["Chon"].ToString() == "True")
                        {
                            dangChon = true;
                            break;
                        }
                    }
                    ContextMenu m = new ContextMenu();
                    if (dangChon)
                    {
                        if (gvKho.GetDataRow(gvKho.GetSelectedRows()[0])["MaKho"].ToString() == "KT")
                            m.MenuItems.Add(new MenuItem("Xuất điều chuyển"));
                        else
                            m.MenuItems.Add(new MenuItem("Xuất chuyển về kho tổng"));
                        m.MenuItems.Add(new MenuItem("Xuất hủy bỏ"));
                        m.MenuItems.Add(new MenuItem("Xuất trả nhà cung cấp"));
                        m.MenuItems.Add(new MenuItem("Xuất sử dụng"));
                        m.MenuItems.Add(new MenuItem("Bỏ chọn hết",menuBoChonHet));
                    }
                    m.MenuItems.Add(new MenuItem("Chọn hết",menuChonHet));
                    m.Show(gctVatTu, new Point(e.X, e.Y));
                }
                catch { }
            }
        }

        private void menuChonHet(object sender, EventArgs e)
        {
            for (int i = 0; i < gvVatTu.RowCount; i++)
            {
                gvVatTu.SetRowCellValue(i, gclChon, "True");
            }
        }

        private void menuBoChonHet(object sender, EventArgs e)
        {
            for (int i = 0; i < gvVatTu.RowCount; i++)
            {
                gvVatTu.SetRowCellValue(i, gclChon, "False");
            }
        }

        private void txtTenVatTu_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                gvVatTu.Columns["TenVatTu"].FilterInfo = new ColumnFilterInfo("Contains([Tên vật tư], '" + XuLy.convertToUnSign3(txtTenVatTu.Text.Trim()) + "')");

                if (txtTenVatTu.Text.Trim() == "")
                    gvVatTu.Columns["TenVatTu"].ClearFilter();
            }
            catch { }
        }

        private void picVatTuClean_Click(object sender, EventArgs e)
        {
            txtTenVatTu.Text = "";
        }

    }
}
